package com.example.springboottools.utils;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadExcel {
    public static List<Map<String,Object>> readExcel1(File file, String[] nameString, int row, int startSheet) {
        try {
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);
            // Excel的页签数量
            int sheet_size = wb.getNumberOfSheets();
            for (int index = startSheet; index < sheet_size; index++) {
                List<Map<String,Object>> outerList=new ArrayList<>();
                // 每个页签创建一个Sheet对象
                Sheet sheet = wb.getSheet(index);
                // sheet.getRows()返回该页的总行数
                //for (int i = 0; i < sheet.getRows(); i++) {
                for (int i = row; i < sheet.getRows(); i++) {
                    //List innerList=new ArrayList();
                    Map<String,Object> innerMap=new HashMap<>();
                    // sheet.getColumns()返回该页的总列数
                    for (int j = 0; j < sheet.getColumns(); j++) {
                        String cellinfo = sheet.getCell(j, i).getContents();
                        if(cellinfo.isEmpty()){
                            continue;
                        }
                        //innerList.add(cellinfo);
                        innerMap.put(nameString[j],cellinfo);
                        // System.out.println(innerMap);
                    }
                    outerList.add(i-row, innerMap);
                    // System.out.println();
                }
                return outerList;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static List<Map<String,Object>> readExcelXss(File file, String[] nameString, int row, int startSheet) {
        try {
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(file);
            // jxl提供的Workbook类
            XSSFWorkbook workbook = new XSSFWorkbook(is);
            // Excel的页签数量
            int sheet_size = workbook.getNumberOfSheets();
            for (int index = startSheet; index < sheet_size; index++) {
                List<Map<String,Object>> outerList=new ArrayList<>();
                // 每个页签创建一个Sheet对象
                XSSFSheet sheetAt = workbook.getSheetAt(index);
                // sheet.getRows()返回该页的总行数
                //for (int i = 0; i < sheet.getRows(); i++) {
                for (int i = row; i <=sheetAt.getLastRowNum(); i++) {
                    //List innerList=new ArrayList();
                    Map<String,Object> innerMap=new HashMap<>();
                    // sheet.getColumns()返回该页的总列数
                    int columnCount = sheetAt.getRow(0).getLastCellNum();
                    for (int j = 0; j < columnCount; j++) {
                        String cellinfo = sheetAt.getRow(i).getCell(j).getStringCellValue();
                        if(cellinfo.isEmpty()){
                            continue;
                        }
                        //innerList.add(cellinfo);
                        innerMap.put(nameString[j],cellinfo);
                        // System.out.println(innerMap);
                    }
                    outerList.add(i-row, innerMap);
                    // System.out.println();
                }
                return outerList;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
